package org.fluidindia.ria.library.controller
{
	import flash.display.DisplayObject;
	
	import mx.containers.ViewStack;
	
	import org.fluidindia.ria.library.event.InitViewEvent;
	import org.fluidindia.ria.library.event.ViewChangeEvent;
	
	/**
	 * <p>
	 * Implements INavigationController to handle primary navigation in the application
	 * built on top of ViewStacks
	 * </p>
	 * 
	 * <p>
	 * Flex provides three navigation controller i.e. ViewStack, TabNavigator and Accordion. This controller will 
	 * use ViewSatcks to handle nvatigations i.e. all primary view / page navigations. 
	 * Primary navigation has been setup as:
	 * 
	 * <pre>
	 *    Main View Stack
	 * 		 View Template
	 * 			Some pages - 1
	 * 			Some Pages - 2
	 * 		 View Template
	 * 			Some pages - 3
	 * 			Some Pages - 4
	 * </pre>
	 * 
	 * This controller will receive a ViewChangeEvent. This event will carry with in itself the details of the view 
	 * that has been requested. The view names are captured in a parent.child format. Example, for View 4, the view name
	 * will be 2.2 i.e. 2nd Child stack and first view in the stack.
	 * </p>
	 * 
	 * <p>
	 * The controller will parse the values and activate the respective view. In addition, this controller will perform
	 * following actions:
	 * <ol>
	 *   <li>Change the Browser's title</li>
	 *   <li>Change the Browser's url - to enable history and page back and forward actions</li>
	 *   <li>Intimate the incoming view of its activation. This will assist the views in reloading themselved.
	 *       This is critical, because once the flex view is created, it is not created again. This results
	 *       in basic action not getting executed again. The view has to be intimated explicitly to refresh itself.
	 *   </li>
	 *   <li>Intimate the outgoing view of its deactivation. This will assist to do any cleanups and releasing any memory.</li>
	 *   <li>Will handle any browser URL requests and reload respective view.</li>
	 * </p>
	 * 
	 */
	public class ViewStackNavigationController implements INavigationController
	{
	 	private static var mainApplicationViewStack:ViewStack;

		/**
		 * In AS3, we can not define private constructors, hence we have an alternate approach to singeton.
		 */
		public function ViewStackNavigationController(mainViewStack:ViewStack)
		{
			// Set the view stack locally. This view stack is used to navigate around.
	 		mainApplicationViewStack = mainViewStack;
		}


		public function changeView(event:ViewChangeEvent):void
		{
			var targetViewName:String = event.targetViewName;
			var originatorViewName:String = event.originatorViewName;
			
			var primaryIndex:int = int(targetViewName.substr(0, 1));
			var secondaryIndex:int = int(targetViewName.substr(2, 1));
			
			// Offset the index, because the view names have been defined as 1 while the index starts at 0.
			primaryIndex = primaryIndex - 1;
			secondaryIndex = secondaryIndex - 1;
			
			mainApplicationViewStack.selectedIndex = primaryIndex;
			var object:Object = mainApplicationViewStack.getChildAt(primaryIndex);
			
			// This method call will change the selectedIndex of the child View stack.
			// Secondary ViewStack should implement this method.
			var targetObject:DisplayObject = object.navigateTo(secondaryIndex);
			
			this.communicateViewActivation(targetObject, event);
		} 
		
		
		public function updateBrowserTitle():void
		{	
		}



		public function updateBrowserURL():void
		{	
		}


		/**
		 * This method will raise an event on the view that is being activated. This will help the view to
		 * execute an initialization actions. The "targetView" should add an event listener on them 
		 * for this to work
		 * 
		 */
		public function communicateViewActivation(targetObject:DisplayObject, event:ViewChangeEvent):void
		{	
			if(targetObject != null)
			{
				targetObject.dispatchEvent(new InitViewEvent(event.originatorViewName, event.information));
			}

		}


		public function communicateViewDeActivation():void
		{	
		}


		public function handleBookmarks():void
		{	
		}

	}
}
